{% extends 'base.html' %}

{% block title %}碳排放因子管理{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>碳排放因子管理</h1>
    <a href="{{ url_for('factor.add_factor') }}" class="btn btn-primary">添加碳排放因子</a>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="get" action="{{ url_for('factor.factor_list') }}" class="d-flex">
            <input type="text" name="search" class="form-control me-2" placeholder="搜索因子名称或来源..." value="{{ search_query }}">
            <button type="submit" class="btn btn-outline-primary">搜索</button>
            {% if search_query %}
                <a href="{{ url_for('factor.factor_list') }}" class="btn btn-outline-secondary ms-2">清除</a>
            {% endif %}
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>因子名称</th>
                        <th>气体种类</th>
                        <th>值类型</th>
                        <th>碳排值</th>
                        <th>来源</th>
                        <th>因子类型</th>
                        <th>分类</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for factor in factors %}
                    <tr>
                        <td>{{ factor.name }}</td>
                        <td>{{ factor.gas_type }}</td>
                        <td>{{ factor.value_type }}</td>
                        <td>{{ factor.emission_value }}</td>
                        <td>{{ factor.source }}</td>
                        <td>{{ factor.factor_type }}</td>
                        <td>{{ factor.category_path }}</td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ url_for('factor.edit_factor', id=factor.id) }}" class="btn btn-outline-primary">编辑</a>
                                <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ factor.id }}">删除</button>
                            </div>
                        </td>
                    </tr>
                    <!-- 删除确认模态框 -->
                    <div class="modal fade" id="deleteModal{{ factor.id }}" tabindex="-1" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title">确认删除</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    <p>确定要删除因子 <strong>{{ factor.name }}</strong> 吗？此操作不可撤销。</p>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                    <form method="post" action="{{ url_for('factor.delete_factor', id=factor.id) }}">
                                        <button type="submit" class="btn btn-danger">删除</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% else %}
                    <tr>
                        <td colspan="8" class="text-center">暂无碳排放因子数据</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}